﻿#define _CRT_SECURE_NO_WARNINGS//qsort排序各种类型的数据
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu
{
	int age;
	char name[20];
};
void print(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
}
void print2(char* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%c ", a[i]);
	}
}
void print3(struct stu* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%s ", a[i].name);
	}
}
int cmp1(const void* p1, const void* p2)
{
	if (*(int*)p1 > *(int*)p2)
		return 1;
	else if (*(int*)p1 < *(int*)p2)
		return -1;
	else
		return 0;

}
int cmp2(const void* p1, const void* p2)
{
	if (*(char*)p1 > *(char*)p2)
		return 1;
	else if (*(char*)p1 < *(char*)p2)
		return -1;
	else
		return 0;

}
int cmp3(const void* p1, const void* p2)
{
	return strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name);
}

void test1()
{
	int a[] = { 2,1,5,4,3 };
	int num = sizeof(a) / sizeof(a[0]);
	int sz = sizeof(a[0]);
	qsort(a, num, sz, cmp1);
	print(a, num);
}
void test2()
{
	char b[] = { 'a','c','d','b','e' };
	int num = sizeof(b) / sizeof(b[0]);
	int sz = sizeof(b[0]);
	qsort(b, num, sz, cmp2);
	print2(b, num);
}

void test3()
{
	struct stu s[] = { {23,"zhangsan"},{21,"lisi"},{22,"wangwu"} };
	int num = sizeof(s) / sizeof(s[0]);
	int sz = sizeof(s[0]);
	qsort(s, num, sz, cmp3);
	print3(s, num);
}
int main()
{
	test1();
	printf("\n");
	test2();
	printf("\n");
	test3();
}